﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.SqlServer.Server;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using System.Configuration;
using SimpleLoggingForNet.Core;


namespace SimpleLoggingForNet.Host.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class SimpleLoggingForNetService : ISimpleLoggingForNetService
    {
        public int LogException(string ExceptionCategory, int EventID, string ExceptionMessage, string ExceptionTitle, int ExceptionSeverity, string WorkStationID)
        {
            try
            {
                BusinessObjects bo = new BusinessObjects();
                return bo.LogException(ExceptionCategory, EventID, ExceptionMessage, ExceptionTitle, ExceptionSeverity, WorkStationID);
            }

            catch (Exception ex)
            {
                string msg = ex.Message;
                //as of now, do nothing.
                return -1;
            }          
        }

        public int LogInformation(int EventID, string Message, string Title, string WorkStationID)
        {
            string sql = "INSERT INTO dbo.Exception ([ID],[ExceptionCategory],[EventID],[ExceptionMessage],[ExceptionSeverity],[ExceptionTitle],[ExceptionApplicationID],[HostID],[ExceptionTimestamp])VALUES(NEWID(),3," + EventID.ToString() + ",'" + Message + "'," + "0" + ",'" + Title + "','93DD98EB-6C4E-4AB2-8FA6-4B35E0D456E2','" + WorkStationID + "',GETDATE())";
            int ret = -1;
            try
            {
                SqlDatabase db = new SqlDatabase(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
                ret = db.ExecuteNonQuery(System.Data.CommandType.Text, sql);
            }

            catch (Exception ex)
            {
                throw new FaultException("An error occurred while logging information", new FaultCode("ERR002"));
                //string msg = ex.Message;
                //ret = 0;
            }

            return ret;
        }
    }
}
